package managedbean;

import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import session.stateless.SystemMessagesLocal;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 * 
 * THIS BEAN IS FOR REDIRECTION PURPOSE ONLY!
 */

@ManagedBean(name="pageRedirectorBean")
@RequestScoped
public class PageRedirectorBean implements Serializable {
    // EXCEPTION
    @EJB
    private SystemMessagesLocal systemMessages;
    
    private WebLocalization localization;
    
    // preferred locale from session
    private String preferredLocale;
   

    /** Creates a new instance of TouristLoginManagedBean */
    public PageRedirectorBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
    }

    // to amelia's branch
    public void goBranchPage() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("stakeholder/index.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    // to weihua's login
    public void goLoginPage() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("Login.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    public void goTouristLoginPage() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("TouristLogin.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    // previous level ../TouristLogin.xhtml
    public void goTouristLoginPageAtPreviousLevel() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("../TouristLogin.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    // previous level ../TouristLogin.xhtml
    public void goStaffLoginPageAtPreviousLevel() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("../StaffLogin.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    public void goStaffLoginPage() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("StaffLogin.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    // in same folder or level
    public void goTouristRegisterPage() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("TouristRegister.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }
    
    public void goStaffRegisterPage() {
        try {
            // send tourist account ID to next page through http request
            FacesContext.getCurrentInstance().getExternalContext().redirect("StaffRegister.xhtml");
        }
        catch (IOException ex) {
            // EXCEPTION
            String statusMessage = systemMessages.getSystemMessage("REDIRECT_FAIL", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }

    public SystemMessagesLocal getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesLocal systemMessages) {
        this.systemMessages = systemMessages;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

}
